//
//  SettingsViewController.m
//  KiklinkApp
//
//  Created by LJC on 14/10/22.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "SettingsViewController.h"

#import "ImagesManager.h"
#import "UIColor+hex.h"
#import "UIView+loadWithNib.h"

#import "SettingsCell.h"

#import "FeedbackViewController.h"
#import "AboutViewController.h"

#import "AccountManager.h"
#import "API.h"

@interface SettingsViewController ()
{
    ImagesManager *imagesManager;
    
    //UI
    UIAlertView *updateAlert;
    UIAlertView *favourableAlert;
}
@end

@implementation SettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    self.view.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    
    
    //BundleImages
    imagesManager = [ImagesManager sharedImagesManager];
    
    
    //leftNavItem
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, NavBarHeight/4, NavBarHeight/2)];
    [backButton setBackgroundImage:[imagesManager back] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[imagesManager backTapped] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftNavBarItem;
    
    
    //logoutBtn
    [self.logoutBtn.layer setMasksToBounds:YES];
    [self.logoutBtn.layer setCornerRadius:5];
    [self.logoutBtn addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    
    
    //settingsTable
    [self.settingsTable setSeparatorInset:UIEdgeInsetsZero];
    self.settingsTable.separatorColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.settingsTable.delegate = self;
    self.settingsTable.dataSource = self;
    self.settingsTable.scrollEnabled = NO;
    
    
    //alertView
    updateAlert = [[UIAlertView alloc] initWithTitle:@"信息" message:@"   您目前的已是最新版本！" delegate:self cancelButtonTitle:@"好" otherButtonTitles:@"给个好评", nil];
    favourableAlert = [[UIAlertView alloc] initWithTitle:@"评价" message:@"去APP STORE给个好评" delegate:self cancelButtonTitle:@"好滴" otherButtonTitles:@"不去了", nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    
    LoginStatus = [AccountManager isKiklinkLoggedin];
    
    if (LoginStatus == YES) {
        self.logoutBtn.hidden = NO;
    } else {
        self.logoutBtn.hidden = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - back
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - back End

#pragma mark - logout
- (void)logout
{
    [AccountManager logoutKiklinkWithCallback:^(int stateCode, NSString *word, id content) {
        if (stateCode == SUCCESS) {
            self.logoutBtn.hidden = YES;
        } else {
            
        }
    }];
}
#pragma mark - logout End

#pragma tableView dataSource&delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:{
            SettingsCell *cell = [SettingsCell loadWithNib:@"SettingsCell" AndPostion:CGPointZero];
            cell.iconImage.image = [imagesManager sharing];
            cell.titleLabel.text = @"告诉朋友";
            return cell;
        }
            break;
        case 1:{
            SettingsCell *cell = [SettingsCell loadWithNib:@"SettingsCell" AndPostion:CGPointZero];
            cell.iconImage.image = [imagesManager update];
            cell.titleLabel.text = @"检查更新";
            return cell;
        }
            break;
        case 2:{
            SettingsCell *cell = [SettingsCell loadWithNib:@"SettingsCell" AndPostion:CGPointZero];
            cell.iconImage.image = [imagesManager feedBack];
            cell.titleLabel.text = @"意见反馈";
            return cell;
        }
            break;
        case 3:{
            SettingsCell *cell = [SettingsCell loadWithNib:@"SettingsCell" AndPostion:CGPointZero];
            cell.iconImage.image = [imagesManager favourable];
            cell.titleLabel.text = @"给个好评";
            return cell;
        }
            break;
        case 4:{
            SettingsCell *cell = [SettingsCell loadWithNib:@"SettingsCell" AndPostion:CGPointZero];
            cell.iconImage.image = [imagesManager about];
            cell.titleLabel.text = @"关于我们";
            return cell;
        }
            break;
        default:{
            return nil;
        }
            break;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    switch (indexPath.row) {
        case 0:{
            [AccountManager openSNSShareUIWithSharedText:@"一起来加入开练网吧！" shareImage:nil];
        }
            break;
        case 1:{
            [updateAlert show];
        }
            break;
        case 2:{
            FeedbackViewController *feedbackVC = [[FeedbackViewController alloc] init];
            [self.navigationController pushViewController:feedbackVC animated:YES];
        }
            break;
        case 3:{
            [favourableAlert show];
        }
            break;
        case 4:{
            AboutViewController *aboutVC = [[AboutViewController alloc] init];
            [self.navigationController pushViewController:aboutVC animated:YES];
        }
            break;
        default:
            break;
    }
}
#pragma tableView dataSource&delegate End

#pragma mark - AlertView delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView == updateAlert) {
        switch (buttonIndex) {
            case 0:
                NSLog(@"检查更新：好");
                break;
            case 1:
                NSLog(@"检查更新：给个好评");
                break;
            default:
                break;
        }
    } else if (alertView == favourableAlert) {
        switch (buttonIndex) {
            case 0:
                NSLog(@"给个好评：好滴");
                break;
            case 1:
                NSLog(@"给个好评：不去了");
                break;
            default:
                break;
        }
    }
}
#pragma mark - AlertView delegate End

@end
